package com.ezjie.ielts.module_personal;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.alibaba.fastjson.JSON;
import com.ezjie.ielts.R;
import com.ezjie.ielts.core.base.BaseBean;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.core.http.HttpRequestAbstractCallBack;
import com.ezjie.ielts.core.spfs.SharedPrefHelper;
import com.ezjie.ielts.db.UserInfoDB;
import com.ezjie.ielts.module_set.FeedbackActivity;
import com.ezjie.ielts.task.ChangeAimTask;
import com.ezjie.ielts.util.AppUtil;
import com.ezjie.ielts.util.LogUtils;
import com.ezjie.ielts.util.NetworkUtil;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.widget.TitleTextView;
import com.ezjie.login.model.UserDetail;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

@ContentView(R.layout.layout_set_aim)
public class ChangeAimActivity extends BaseFragmentActivity implements OnCheckedChangeListener {

	public class DayAimUtil {
		public static final int ERROR_AIM_10 = 10;
		public static final int ERROR_AIM_20 = 20;
		public static final int ERROR_AIM_30 = 30;
	}

	@ViewInject(R.id.tv_topbar_title)
	private TitleTextView tv_topbar_title;
	@ViewInject(R.id.iv_topbar_back)
	private ImageView iv_topbar_back;

	@ViewInject(R.id.error_radio_group)
	private RadioGroup error_radio_group;
	@ViewInject(R.id.error_radio_1)
	private RadioButton error_radio_1;
	@ViewInject(R.id.error_radio_2)
	private RadioButton error_radio_2;
	@ViewInject(R.id.error_radio_3)
	private RadioButton error_radio_3;
	@ViewInject(R.id.sort_radio_group)
	private RadioGroup sort_radio_group;
	@ViewInject(R.id.sort_radio_1)
	private RadioButton sort_radio_1;
	@ViewInject(R.id.sort_radio_2)
	private RadioButton sort_radio_2;

	@ViewInject(R.id.chanage_aim_button)
	private Button chanage_aim_button;

	private int mErrorAimNum;
	private ChangeAimTask changeAimTask;
	private UserInfoDB userInfoDB;
	private UserDetail userDetail;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		changeAimTask = new ChangeAimTask(this);
		userInfoDB = new UserInfoDB(this);
		userDetail = userInfoDB.queryLoginUser();
		init();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_CHANGE_AIM);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_CHANGE_AIM);
	}

	private void init() {
		tv_topbar_title.setText(R.string.personal_myaim);
		iv_topbar_back.setOnClickListener(this);

		if (userDetail != null) {
			String dailyNum = SharedPrefHelper.getInstance().getDailyNum(userDetail.uid);
			String rand = SharedPrefHelper.getInstance().isRand(userDetail.uid);

			if (dailyNum.equals(DayAimUtil.ERROR_AIM_10 + "")) {
				error_radio_1.setChecked(true);
				mErrorAimNum = DayAimUtil.ERROR_AIM_10;
			} else if (dailyNum.equals(DayAimUtil.ERROR_AIM_20 + "")) {
				error_radio_2.setChecked(true);
				mErrorAimNum = DayAimUtil.ERROR_AIM_20;
			} else if (dailyNum.equals(DayAimUtil.ERROR_AIM_30 + "")) {
				error_radio_3.setChecked(true);
				mErrorAimNum = DayAimUtil.ERROR_AIM_30;
			}
			if ("1".equals(rand)) {
				sort_radio_1.setChecked(true);
			} else {
				sort_radio_2.setChecked(true);
			}
		}

		error_radio_group.setOnCheckedChangeListener(this);
		sort_radio_group.setOnCheckedChangeListener(this);
		chanage_aim_button.setOnClickListener(this);
	}

	@Override
	public void onClickEvent(View view) {
		switch (view.getId()) {
		case R.id.iv_topbar_back:
			finish();
			break;
		case R.id.chanage_aim_button:
			// 友盟统计
			MobclickAgent.onEvent(this, "changeGlobal_submit");

			ChangeAim();
			break;
		default:
			break;
		}
	}

	private void ChangeAim() {
		if (!NetworkUtil.isNetworkAvailable(this)) {
			AppUtil.showToast(this, R.string.no_network);
			return;
		}

		changeAimTask.changeAim(sort_radio_1.isChecked() ? "1" : "0", mErrorAimNum + "",
				new HttpRequestAbstractCallBack(this,true) {
					@Override
					public void onStartCallBack() {
						// TODO Auto-generated method stub
						super.onStartCallBack();
						AppUtil.showProgressDialog(ChangeAimActivity.this);
					}

					@Override
					public void onSuccessCallBack(String result) {
						// TODO Auto-generated method stub
						super.onSuccessCallBack(result);
						AppUtil.dismissProgressDialog();
						LogUtils.d(result);
						try {
							BaseBean baseBean = JSON.parseObject(result, BaseBean.class);
							if (baseBean != null) {
								AppUtil.showToast(ChangeAimActivity.this, baseBean.getMsg());
								if (userDetail != null) {
									SharedPrefHelper.getInstance().setIsRand(userDetail.uid,
											sort_radio_1.isChecked() ? "1" : "0");
									SharedPrefHelper.getInstance().setDailyNum(userDetail.uid, mErrorAimNum + "");
								}

								ChangeAimActivity.this.finish();
							}
						} catch (Exception e) {
							LogUtils.exception(e);
						}
					}

					@Override
					public void onFailureCallBack(HttpException error, String msg) {
						// TODO Auto-generated method stub
						super.onFailureCallBack(error, msg);
						AppUtil.dismissProgressDialog();
						AppUtil.showToast(ChangeAimActivity.this, R.string.load_net_data_failure);
					}
				});
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (group.getId()) {
		case R.id.error_radio_group:
			// 友盟统计
			MobclickAgent.onEvent(this, "changeGlobal_radioChange");
			if (checkedId == R.id.error_radio_1) {
				mErrorAimNum = DayAimUtil.ERROR_AIM_10;
			} else if (checkedId == R.id.error_radio_2) {
				mErrorAimNum = DayAimUtil.ERROR_AIM_20;
			} else if (checkedId == R.id.error_radio_3) {
				mErrorAimNum = DayAimUtil.ERROR_AIM_30;
			}
			break;
		case R.id.sort_radio_group:
			// 友盟统计
			MobclickAgent.onEvent(this, "changeGlobal_checkoutChange");
			if (checkedId == R.id.sort_radio_1) {

			} else if (checkedId == R.id.sort_radio_2) {

			}
			break;
		default:
			break;
		}

	}

}
